package com.chengh.nami.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.request.PageQuery;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.utils.FileUtils;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.domain.dto.LogininforDto;
import com.chengh.nami.system.mapper.SysLoginforMapper;
import com.chengh.nami.system.pojo.SysLogininfor;
import com.chengh.nami.system.service.ISysLogininforService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 登陆日志服务类
 *
 * @author chengh
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLoginforMapper, SysLogininfor> implements ISysLogininforService {

    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        this.save(logininfor);
    }

    @Override
    public PageData<SysLogininfor> getList(LogininforDto logininforDto, PageQuery pageQuery) {
        Page<SysLogininfor> page = pageQuery.build();
        LambdaQueryWrapper<SysLogininfor> queryWrapper = new LambdaQueryWrapper<SysLogininfor>()
                .eq(StringUtils.isNotEmpty(logininforDto.getStatus()), SysLogininfor::getStatus, logininforDto.getStatus())
                .like(StringUtils.isNotEmpty(logininforDto.getUserName()), SysLogininfor::getUserName, logininforDto.getUserName())
                .ge(logininforDto.getBeginTime() != null, SysLogininfor::getLoginTime, logininforDto.getBeginTime())
                .le(logininforDto.getEndTime() != null, SysLogininfor::getLoginTime, logininforDto.getEndTime());
        Page<SysLogininfor> logininforPage = baseMapper.selectPage(page, queryWrapper);
        return PageData.build(logininforPage);
    }

    @Override
    public void cleanLogininfor() {
        baseMapper.cleanLogininfor();
    }

    @Override
    public void exportLogininforExcle(HttpServletResponse response, LogininforDto logininforDto, PageQuery pageQuery) {
        PageData<SysLogininfor> listPage = this.getList(logininforDto, pageQuery);
        List<SysLogininfor> list = listPage.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            new BizException("查询到的结果为空");
        }
        List<Map<String, Object>> logininforList = new ArrayList<>();
        for (SysLogininfor logininfor : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("编号", logininfor.getId());
            map.put("用户账号", logininfor.getUserName());
            String status = logininfor.getStatus();
            map.put("登录状态", "0".equals(status) ? "成功" : "失败");
            map.put("登录IP地址", logininfor.getIpaddr());
            map.put("登录地点", logininfor.getLoginLocation());
            map.put("浏览器类型", logininfor.getBrowser());
            map.put("操作系统", logininfor.getOs());
            map.put("提示消息", logininfor.getMsg());
            map.put("访问时间", logininfor.getLoginTime());
            logininforList.add(map);
        }
        try {
            FileUtils.downloadExcel(logininforList, response);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
